<div class="layui-fluid layui-anim website-anim-up" id="website-review-guide-detail" lay-title="查看评审信息">
    <div class="layui-row layui-col-space8 website-container">
        <div class="layui-col-md10 layui-col-sm10 layui-col-xs10">
            <div class="layui-card">
                <div class="layui-card-header">评审结果</div>
                <div class="layui-card-body layui-form-item" id="review-guide">
                    <div class="layui-row">
                        <table lay-filter="reviewResultTable" id="reviewResultTable"
                               lay-data="{id: 'reviewResultTable'}"></table>
                    </div>
                    <form class="layui-form layui-form-item">
                        <div class="layui-row">
                            <label class="layui-form-label">上传附件：</label>
                            <div class="layui-inline layui-col-xs6 layui-col-sm6 layui-col-md6">
                                <a id="upload-file-a"></a>
                                <button type="button" class="layui-btn" id="upload-file">
                                    <i class="layui-icon">&#xe67c;</i>上传附件
                                </button>
                            </div>
                        </div>
                        <div class="layui-row">
                            <label class="layui-form-label website-form-item-require">评审意见：</label>
                            <div class="layui-inline layui-col-xs5 layui-col-sm5 layui-col-md12">
                                <textarea class="layui-textarea" id="researchContent" name="researchContent"
                                          autocomplete="off"></textarea>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <div class="layui-row">
                <button class="layui-btn" id="submit">提交</button>
                <button class="layui-btn" id="save">保存</button>
                <button class="layui-btn layui-btn-warm" id="clean">返回</button>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="subjectTypeCodes-form-name-type">
    {{#
    var dictList=layui.dict.getByCodes(d.subjectTypeCodes);
    }}
    <span>
    {{# layui.each(dictList,function(index,item) { }}
    {{ item.name }}&nbsp;
    {{#  }); }}
    </span>
</script>

<script type="text/html" id="topicClassifyCode-name-type">
    {{#
    let topicClassifyCode=layui.dict.getByCode(d.topicClassifyCode);
    }}
    {{#  if(topicClassifyCode){ }}
    <span>{{ topicClassifyCode.name }}</span>
    {{#  } }}
</script>
<script type="text/html" id="topicLevelType-name-type">
    {{#
    let dict=layui.dict.getByCode(d.topicLevelType);
    }}
    {{#  if(dict){ }}
    <span>{{ dict.name }}</span>
    {{#  } }}
</script>
<script data-th-inline="javascript" type="text/javascript">
    layui.use(['jquery', 'laydate', 'form', 'website', 'dict', 'upload', 'clientModules'], function () {
        let $ = layui.jquery,
            laydate = layui.laydate,
            website = layui.website,
            form = layui.form,
            view = layui.view,
            upload = layui.upload,
            client = layui.clientModules,

            $view = $('#website-review-guide-detail'),
            $reviewView = $('#review-guide'),
            $clean = $('#clean'),
            $submit = $('#submit'),
            $save = $('#save'),

            distributionRecordId = [[${distributionRecordId}]],
            year = [[${year}]],
            record = [[${record}]],
            reviewFinished = record.reviewFinished,
            planId = record.planId,

            reviewResultTable,
            uploadFile;

        $clean.on('click', function () {
            let currentUrl = window.location.hash.replace('#', '');
            website.view.tab.del(currentUrl);
            window.location.reload();
        })
        initReviewResultTable();
        initForm()

        uploadFile = upload.render({
            elem: '#upload-file' //绑定元素
            , url: '/upload' //上传接口
            , done: function (res) {
                //上传完毕回调
                if (res.code === 200) {
                    website.alert.success('上传文件成功');
                    let data = res.data
                    $('#upload-file')[0].setAttribute('file-id', data.fileId)
                    $('#upload-file-a')[0].setAttribute('href', ctx + 'download/' + data.fileId)
                    $("#upload-file-a").text(data.name)
                }
            }
            , error: function () {
                //请求异常回调
                website.alert.error('上传文件失败');
            },
            accept: 'file',
            size: 1024 * 50
        });

        if (reviewFinished) {
            $('#upload-file').hide()
            $("#researchContent")[0].setAttribute('readonly', 'readonly')
            $submit.hide()
            $save.hide()
        }


        function initReviewResultTable() {

            reviewResultTable = website.table.init({
                elem: $reviewView.find('table'),
                id: 'reviewResultTable',
                url: ctx + 'biz/topic/list',
                where: {year: year},
                cols: [
                    [
                        {type: 'numbers', title: '序号'},
                        {title: '课题名称', minWidth: 100, field: 'topicName'},
                        {field: 'year', title: '年份'},
                        {templet: '#topicClassifyCode-name-type', title: '课题类型'},
                        {templet: '#topicLevelType-name-type', title: '课题分类'},
                        {title: '学科分类', templet: '#subjectTypeCodes-form-name-type', minWidth: 100},
                        // {title: '操作', toolbar: '#guide-option', minWidth: 200}
                    ]
                ]
            });
        }

        function initForm() {
            if (record) {
                if (record.fileId) {
                    $("#upload-file-a").text('点击下载');
                    $("#upload-file-a")[0].setAttribute('href', ctx + 'download/' + record.fileId)
                } else {
                    $("#upload-file-a").text('无附件');
                }
                $("#researchContent").text(record.reviewText ? record.reviewText : '');
            }
        }


        // 提交
        $submit.on('click', function () {
            client.checkSubmit(sendAJAX, true)
            return false
        })


        // $save 0 为保存、1 为提交
        $save.on('click', function () {
            sendAJAX(false)
            return false
        })

        let sendAJAX = function (isSubmit) {

            let researchContent = $('#researchContent').val(),
                sendData = {distributionRecordId: distributionRecordId, planId: planId}
            if (!researchContent) {
                website.alert.error('请填写评审意见');
                return false;
            }
            sendData.reviewText = researchContent
            sendData.fileId = $('#upload-file')[0].getAttribute('file-id')
            if (isSubmit) {
                sendData.reviewFinishedDate = new Date()
                sendData.reviewFinished = true
            }
            const json = JSON.stringify(sendData)
            website.ajaxPost(ctx + 'classDistributionRecord/update', json, function (data) {
                let currentUrl = window.location.hash.replace('#', '');
                website.view.tab.del(currentUrl);
                window.location.reload();
                //  关闭当前页面
                $('#website-home').find('#query').click();
            });
        }

    })


</script>